package com.uubx.controller;


import com.uubx.exception.CustomerException;
import com.uubx.pojo.User;
import com.uubx.result.MessageCode;
import com.uubx.result.Result;
import com.uubx.result.ResultUtils;
import com.uubx.service.UserLoginService;
import com.uubx.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author wyy
 * @Date 2020/12/28 21:52
 * @Version 1.0
 */
@RequestMapping("/user")
@ResponseBody
@Controller
@Api(description = "登入相关的接口文档")
public class LoginController {

    @Autowired
    private UserLoginService userLoginService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation(value = "登入",notes = "登入",httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result sendLoginSms(@RequestBody LoginVo loginVo) throws CustomerException {
       //判断数据是否为空
        if(loginVo==null){
            throw new CustomerException(MessageCode.LOFIN_EMPTY);
        }
        //判断异常
        User user=null;
        try {
             user = userLoginService.login(loginVo.getUsername(), loginVo.getPassword());
        }catch (CustomerException e){
            e.printStackTrace();
            return ResultUtils.buildFail(e.getMessageCode());
        }

        return ResultUtils.buildSuccess(user);
    }


    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    @ApiOperation(value = "退出登入",notes = "退出登入",httpMethod = "GET")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result logout(String token){
        userLoginService.loginOut(token);
        return ResultUtils.buildSuccess();
    }


}
